package com.itheima.controller.backend;

import com.itheima.DTO.OrderSettingDTO;
import com.itheima.VO.OrderSettingVO;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;
//import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "预约设置管理")
public class OrdersettingController {

    @Autowired
    private OrdersettingService orderSettingService;


    @GetMapping("/download")
    @ApiOperation("模板下载")
    public void export(HttpServletResponse response,@RequestParam String filename) {
        orderSettingService.download(response,filename);
    }

    @PostMapping("/editNumberByOrderDate")
    @ApiOperation("根据日期修改可预约人数")
    public Result editNumberByOrderDate(@RequestBody OrderSetting OrderSetting) {
        log.info("根据日期修改可预约人数:{}",OrderSetting.getOrderDate());
        if (orderSettingService.editNumberByOrderDate(OrderSetting) == 1) {
            log.info("预约设置修改成功");
            return new Result(true,"预约设置修改成功");
        } else {
            log.info("预约设置修改失败");
            return new Result(false,"预约设置修改失败");
        }

    }

    @GetMapping(value ="getOrderSettingByMonth")
    @ApiOperation("根据年月查询对应预约信息")
    public Result getOrderByMonth(@RequestParam String month){
        log.info("根据年月查询对应预约信息:{}",month);
        String[] result = month.split("-");
        if(result[1].length() == 1){
            month = result[0]+"-"+"0"+result[1];
        }
        log.info("根据年月查询对应预约信息->:{}",month);
        List<OrderSetting> list = orderSettingService.getOrderSettingByMonth(month);
        List<OrderSettingVO> obj= new ArrayList<OrderSettingVO>();
        for(OrderSetting orderSetting:list){
            String[] orderDate = orderSetting.getOrderDate().split("-");
            orderDate[2] = orderDate[2].length() == 1 ? "0"+orderDate[2] : orderDate[2];

            OrderSettingVO orderSettingVO = OrderSettingVO.builder()
//                    .date(orderSetting.getId())
                    .date(Integer.valueOf(orderDate[2]))
                    .number(orderSetting.getNumber())
                    .reservations(orderSetting.getReservations())
                    .build();
            obj.add(orderSettingVO);
        }

        return new Result(true,"查询预约信息成功",obj);
    }


    @PostMapping("/upload")
    @ApiOperation("预约设置批量导入")
    public Result upload(@RequestParam("excelFile") MultipartFile file) {
        log.info("开始处理上传的Excel文件: {}", file.getOriginalFilename());

        try {
            // 调用服务层方法处理Excel文件
            if (orderSettingService.uploadFromExcel(file) == 1) {
                log.info("预约设置批量导入成功");
                return new Result(true, "预约设置批量导入成功");
            } else {
                log.info("预约设置批量导入失败");
                return new Result(false, "预约设置批量导入失败");
            }
        } catch (Exception e) {
            log.error("处理Excel文件时发生错误", e);
            return new Result(false, "处理Excel文件时发生错误");
        }

    }








}